package blockQueue;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue block = new ArrayBlockingQueue(3);
        //队列满抛出异常 add remove
//        System.out.println(block.add("a"));
//        System.out.println(block.add("b"));
//        System.out.println(block.add("c"));
//        System.out.println(block.add("d"));
//        System.out.println("==============");
//        System.out.println(block.remove());
//        System.out.println(block.remove());
//        System.out.println(block.remove());
        //不抛异常 offer   poll
//        System.out.println(block.offer("a"));
//        System.out.println(block.offer("b"));
//        System.out.println(block.offer("c"));
//        System.out.println(block.poll());
//        System.out.println(block.poll());
//        System.out.println(block.poll());
//        System.out.println(block.poll());
        //异常等待
//        block.put("a");
//        block.put("b");
//        block.put("c");
//        System.out.println("=========");
//        System.out.println(block.take());
//        System.out.println(block.take());
//        System.out.println(block.take());
//        System.out.println(block.take());
        //异常超时等待
//        System.out.println(block.offer("a"));
//        System.out.println(block.offer("b"));
//        System.out.println(block.offer("c"));
//        System.out.println(block.poll());
//        System.out.println(block.poll());
//        System.out.println(block.poll());
//        System.out.println(block.poll(3, TimeUnit.SECONDS));
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName() + " put 1");
                block.offer("1");
                System.out.println(Thread.currentThread().getName() + " put 2");
                block.offer("2");
                System.out.println(Thread.currentThread().getName() + " put 3");
                block.offer("3");
                System.out.println(Thread.currentThread().getName() + " put 4");
                block.offer("4", 3, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName() + "get= 1");
                block.poll( 3, TimeUnit.SECONDS);
                System.out.println(Thread.currentThread().getName() + "get= 2");
                block.poll(3, TimeUnit.SECONDS);
                System.out.println(Thread.currentThread().getName() + "get= 3");
                block.poll(3, TimeUnit.SECONDS);
                System.out.println(Thread.currentThread().getName() + "get= 4");
                block.take();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
